package com.hissy.common.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.junit.Test;

/**
 * Created by zhangyanfei on 2019/3/19.
 *
 * @author zhangyanfei
 * @date 2019/03/19
 */
public class DomUtil {
    /**
     * 解析XML
     *
     * getRootElement
     *
     * selectNodes
     */
    @Test
    public void resolving() throws Exception {
        SAXReader saxReader = new SAXReader();
        Document document = saxReader.read(new FileInputStream(new File("Student.xml")));
        System.out.println("Root element:" + document.getRootElement().getName());

        List<Node> nodeList = document.selectNodes("/class/student");

        for (Node node : nodeList) {
            System.out.println(node.getName());
            System.out.println(node.valueOf("@rollno"));
            System.out.println("First Name : " + node.selectSingleNode("firstname").getText());
            System.out.println("Last Name : " + node.selectSingleNode("lastname").getText());
            System.out.println("First Name : " + node.selectSingleNode("nickname").getText());
            System.out.println("Marks : " + node.selectSingleNode("marks").getText());
        }
    }

    /**
     * 创建
     *
     * addElement
     *
     * addAttribute
     *
     * addText
     */
    @Test
    public void createXml() throws Exception {
        Document document = DocumentHelper.createDocument();
        Element root = document.addElement("cars");
        Element superElement = root.addElement("superCars").addAttribute("company", "chan");
        superElement.addElement("carName").addAttribute("type", "10086")
            .addText("100087");
        superElement.addElement("carName").addAttribute("type", "10086")
            .addText("100089");

        OutputFormat outputFormat = OutputFormat.createPrettyPrint();
        XMLWriter writer = new XMLWriter(new FileOutputStream(new File("demo.xml")), outputFormat);
        writer.write(document);
    }

    /**
     * 迭代器
     * elementIterator
     *
     * @throws Exception
     */
    @Test
    public void updateXml() throws Exception {
        SAXReader saxReader = new SAXReader();
        Document document = saxReader.read(new FileInputStream(new File("Student.xml")));

        List<Node> nodeList = document.selectNodes("/class/student");

        for (Node node : nodeList) {
            Element element = (Element)node;

            Iterator<Element> iterator = element.elementIterator("marks");

            while (iterator.hasNext()) {
                Element element1 = iterator.next();
                element1.setText("100");
            }

            OutputFormat format = OutputFormat.createPrettyPrint();
            XMLWriter writer;
            writer = new XMLWriter(System.out, format);
            writer.write(document);

        }
    }
}
